<?php
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
include '../connection/connection.php';

$idmonitor = $_REQUEST['monitor'];

	$query = "SELECT mn.*,fa.*,cardis.*,mn.Pos as posmonitor,fa.pos as posfav,confc.camera_type,confc.lens_type,confc.trigger_type FROM monitor mn, favourite fa,
		 cardisplay cardis,configcamera confc 
		WHERE mn.Monitor_ID = fa.monitor_ID AND cardis.id = confc.id 
		AND fa.camera_ID = cardis.id AND mn.Monitor_ID = $idmonitor ORDER BY fa.pos ASC";
	$selectpos = mysql_query($query);
	
	 while( $row=mysql_fetch_array($selectpos) )
		{
		  $posts[] = $row[posfav];
		  $filename[] = $row[filename];
		  $host[] = $row[host];
		  
		  $oDate = strtotime($row[car_datetime]);
		  $calendar[] = date("Ymd",$oDate);
		  $year[] =date("Y",$oDate);
		  $hours[] =date("H",$oDate);
		  $minutes[] =date("i",$oDate);
		  
		  $dtime[] = $row[car_datetime];
		  $distance[] = $row[camera_type]."-".$row[lens_type];
		  $namecar[] = $row[name];
		  $trigertype[] = $row[id]."/".substr($row[trigger_type], 0,1);
		  $platecar[] =explode("_",$row[filename]);
		  
		  $timecurrent[] = time() - ($row[timeOut]*60*60);
		  $timedatabase[] = strtotime($row[car_datetime]);
		  
		  $mangcha=array();
		  if (isset($_REQUEST['monitorone'])){
		 	 for ($i=0;$i<1;$i++){
			  	$mangcon=array();
			  	$mangcon['carname']=$namecar[$i];
			  	$mangcon['datetime']=$dtime[$i];
			  	$mangcon['image']="http://".$host[$i]."/".$year[$i]."/".$calendar[$i]."/".$hours[$i]."/".$minutes[$i]."/".$filename[$i];
			  	$mangcon['distance']=$distance[$i];
			  	$mangcon['trigertype'] = $trigertype[$i];
			  	$mangcon['platecar'] = str_replace('+','',$platecar[$i][4]);
			  	$mangcon['timecurrent'] = $timecurrent[$i];
			  	$mangcon['timedatabase'] = $timedatabase[$i];
			  	$mangcon['posts'] = $posts[$i];
			  	$khoa="row".$i;
			  	$mangcha[$khoa]=$mangcon;
			  }
		  }else if (isset($_REQUEST['monitor4'])){
		   for ($i=0;$i<=3;$i++){
			  	$mangcon=array();
			  	$mangcon['carname']=$namecar[$i];
			  	$mangcon['datetime']=$dtime[$i];
			  	$mangcon['image']="http://".$host[$i]."/".$year[$i]."/".$calendar[$i]."/".$hours[$i]."/".$minutes[$i]."/".$filename[$i];
			  	$mangcon['distance']=$distance[$i];
			  	$mangcon['trigertype'] = $trigertype[$i];
			  	$mangcon['platecar'] = str_replace('+','',$platecar[$i][4]);
			  	$mangcon['timecurrent'] = $timecurrent[$i];
			  	$mangcon['timedatabase'] = $timedatabase[$i];
			  	$mangcon['posts'] = $posts[$i];
			  	$khoa="row".$i;
			  	$mangcha[$khoa]=$mangcon;
			  }
		  }else if (isset($_REQUEST['monitor9'])){
		    for ($i=0;$i<=8;$i++){
			  	$mangcon=array();
			  	$mangcon['carname']=$namecar[$i];
			  	$mangcon['datetime']=$dtime[$i];
			  	$mangcon['image']="http://".$host[$i]."/".$year[$i]."/".$calendar[$i]."/".$hours[$i]."/".$minutes[$i]."/".$filename[$i];
			  	$mangcon['distance']=$distance[$i];
			  	$mangcon['trigertype'] = $trigertype[$i];
			  	$mangcon['platecar'] = str_replace('+','',$platecar[$i][4]);
			  	$mangcon['timecurrent'] = $timecurrent[$i];
			  	$mangcon['timedatabase'] = $timedatabase[$i];
			  	$mangcon['posts'] = $posts[$i];
			  	$khoa="row".$i;
			  	$mangcha[$khoa]=$mangcon;
			  }
		  }else if (isset($_REQUEST['monitor16'])){
		   for ($i=0;$i<=15;$i++){
			  	$mangcon=array();
			  	$mangcon['carname']=$namecar[$i];
			  	$mangcon['datetime']=$dtime[$i];
			  	$mangcon['image']="http://".$host[$i]."/".$year[$i]."/".$calendar[$i]."/".$hours[$i]."/".$minutes[$i]."/".$filename[$i];
			  	$mangcon['distance']=$distance[$i];
			  	$mangcon['trigertype'] = $trigertype[$i];
			  	$mangcon['platecar'] = str_replace('+','',$platecar[$i][4]);
			  	$mangcon['timecurrent'] = $timecurrent[$i];
			  	$mangcon['timedatabase'] = $timedatabase[$i];
			  	$mangcon['posts'] = $posts[$i];
			  	$khoa="row".$i;
			  	$mangcha[$khoa]=$mangcon;
			  }
		  }else if (isset($_REQUEST['monitor25'])){
		 	 for ($i=0;$i<=24;$i++){
			  	$mangcon=array();
			  	$mangcon['carname']=$namecar[$i];
			  	$mangcon['datetime']=$dtime[$i];
			  	$mangcon['image']="http://".$host[$i]."/".$year[$i]."/".$calendar[$i]."/".$hours[$i]."/".$minutes[$i]."/".$filename[$i];
			  	$mangcon['distance']=$distance[$i];
			  	$mangcon['trigertype'] = $trigertype[$i];
			  	$mangcon['platecar'] = str_replace('+','',$platecar[$i][4]);
			  	$mangcon['timecurrent'] = $timecurrent[$i];
			  	$mangcon['timedatabase'] = $timedatabase[$i];
			  	$mangcon['posts'] = $posts[$i];
			  	$khoa="row".$i;
			  	$mangcha[$khoa]=$mangcon;
			  }
		  }else if (isset($_REQUEST['monitor36'])){
			  for ($i=0;$i<=35;$i++){
			  	$mangcon=array();
			  	$mangcon['carname']=$namecar[$i];
			  	$mangcon['datetime']=$dtime[$i];
			  	$mangcon['image']="http://".$host[$i]."/".$year[$i]."/".$calendar[$i]."/".$hours[$i]."/".$minutes[$i]."/".$filename[$i];
			  	$mangcon['distance']=$distance[$i];
			  	$mangcon['trigertype'] = $trigertype[$i];
			  	$mangcon['platecar'] = str_replace('+','',$platecar[$i][4]);
			  	$mangcon['timecurrent'] = $timecurrent[$i];
			  	$mangcon['timedatabase'] = $timedatabase[$i];
			  	$mangcon['posts'] = $posts[$i];
			  	$khoa="row".$i;
			  	$mangcha[$khoa]=$mangcon;
			  }
		  }else if (isset($_REQUEST['monitor49'])){
			  for ($i=0;$i<=48;$i++){
			  	$mangcon=array();
			  	$mangcon['carname']=$namecar[$i];
			  	$mangcon['datetime']=$dtime[$i];
			  	$mangcon['image']="http://".$host[$i]."/".$year[$i]."/".$calendar[$i]."/".$hours[$i]."/".$minutes[$i]."/".$filename[$i];
			  	$mangcon['distance']=$distance[$i];
			  	$mangcon['trigertype'] = $trigertype[$i];
			  	$mangcon['platecar'] = str_replace('+','',$platecar[$i][4]);
			  	$mangcon['timecurrent'] = $timecurrent[$i];
			  	$mangcon['timedatabase'] = $timedatabase[$i];
			  	$mangcon['posts'] = $posts[$i];
			  	$khoa="row".$i;
			  	$mangcha[$khoa]=$mangcon;
			  }
		  }
		  else if (isset($_REQUEST['monitor64'])){
		   for ($i=0;$i<=63;$i++){
			  	$mangcon=array();
			  	$mangcon['carname']=$namecar[$i];
			  	$mangcon['datetime']=$dtime[$i];
			  	$mangcon['image']="http://".$host[$i]."/".$year[$i]."/".$calendar[$i]."/".$hours[$i]."/".$minutes[$i]."/".$filename[$i];
			  	$mangcon['distance']=$distance[$i];
			  	$mangcon['trigertype'] = $trigertype[$i];
			  	$mangcon['platecar'] = str_replace('+','',$platecar[$i][4]);
			  	$mangcon['timecurrent'] = $timecurrent[$i];
			  	$mangcon['timedatabase'] = $timedatabase[$i];
			  	$mangcon['posts'] = $posts[$i];
			  	$khoa="row".$i;
			  	$mangcha[$khoa]=$mangcon;
			  }
		  }
		}
		mysql_free_result( $selectpos );
		
		if (isset($_REQUEST['monitorone'])){
			echo "<table id='tb'>";
		 $height=50;
		
		 $tongso=1;
		 $cot=1;
		 $vitri=0;
		 foreach( $mangcha as $khoa=>$giatri )
		{
			if($vitri%$cot==0)
			echo "<tr  height='".$height."'>";
			?>
				
	              <td style='padding-right:5px;display:block;position:relative;float:left;width: 1575px;'>
		              <img src="<?=$giatri['image']?>" style="width: 1575px;height:650px;">
		              <span style='left:8px;position:absolute;top:0px;color:yellow;font-size:45px;padding-top:4px;'><?php echo $giatri['carname']?></span>
		              <span style='right:8px;position:absolute;top:0px;color:yellow;'>
		              <?php if ($giatri['timecurrent']<($giatri['timedatabase'])) {?>
		              	<img src='images/circle_green.png' style='width:18px;height:18px;padding-top:4px;'>
		              	<?php }else if ($giatri['timecurrent']>($giatri['timedatabase'])){?>
		              		<img src='images/circle_red.png' style='width:18px;height:18px;padding-top:4px;'>
		              <?php }?>
		              </span>
		              <span style='left:8px;position:absolute;top:536px;color:yellow;font-size:45px;'><?=$giatri['datetime']?></span>
		              <span style='left:8px;position:absolute;top:590px;color:yellow;font-size:45px;'><?=$giatri['platecar']?></span>
		              <span style='right:8px;position:absolute;top:470px;color:yellow;font-size:45px;'><?=$giatri['distance']?></span>
		              <span style='right:8px;position:absolute;top:536px;color:yellow;font-size:45px;'><?=$giatri['trigertype']?></span>
		              <span style='right:8px;position:absolute;top:590px;color:yellow;font-size:45px;'><?=($vitri+1)?></span>
	              </td>
         <?php 
           $vitri++;
           if($vitri%$cot==0)
         	echo "</tr>";
		}
			echo "</table>";
			echo "</p>\n";
		
		}else if (isset($_REQUEST['monitor4'])){
			echo "<table id='tb'>";
		 $height=50;
		
		 $tongso=4;
		 $cot=2;
		 $vitri=0;
		 foreach( $mangcha as $khoa=>$giatri )
		{
			if($vitri%$cot==0)
			echo "<tr  height='".$height."'>";
			?>
				
	              <td style='padding-right:5px;display:block;position:relative;float:left;width:760px;'>
		              <img src="<?=$giatri['image']?>"  style='width:760px;height:325px'>
		              <span style='left:8px;position:absolute;top:0px;color:yellow;font-size:30px;padding-top:4px;'><?php echo $giatri['carname']?></span>
		              <span style='right:8px;position:absolute;top:0px;color:yellow;'>
		              <?php if ($giatri['timecurrent']<($giatri['timedatabase'])) {?>
		              	<img src='images/circle_green.png' style='width:18px;height:18px;padding-top:4px;'>
		              	<?php }else if ($giatri['timecurrent']>($giatri['timedatabase'])){?>
		              		<img src='images/circle_red.png' style='width:18px;height:18px;padding-top:4px;'>
		              <?php }?>
		              </span>
		              <span style='left:8px;position:absolute;top:230px;color:yellow;font-size:30px;'><?=$giatri['datetime']?></span>
		              <span style='left:8px;position:absolute;top:280px;color:yellow;font-size:30px'><?=$giatri['platecar']?></span>
		              <span style='right:8px;position:absolute;top:183px;color:yellow;font-size:30px;'><?=$giatri['distance']?></span>
		              <span style='right:8px;position:absolute;top:230px;color:yellow;font-size:30px'><?=$giatri['trigertype']?></span>
		              <span style='right:8px;position:absolute;top:280px;color:yellow;font-size:30px'><?=($vitri+1)?></span>
	              </td>
         <?php 
           $vitri++;
           if($vitri%$cot==0)
         	echo "</tr>";
		}
			echo "</table>";
			echo "</p>\n";
		}else if (isset($_REQUEST['monitor9'])){
			echo "<table id='tb'>";
		 $height=50;
		
		 $tongso=9;
		 $cot=3;
		 $vitri=0;
		 foreach( $mangcha as $khoa=>$giatri )
		{
			if($vitri%$cot==0)
			echo "<tr  height='".$height."'>";
			?>
				
	              <td style='padding-right:5px;display:block;position:relative;float:left;width: 510px;'>
		              <img src="<?=$giatri['image']?>"  style='width:510px;height:215px'>
		              <span style='left:8px;position:absolute;top:0px;color:yellow;font-size:23px;padding-top:4px;'><?php echo $giatri['carname']?></span>
		              <span style='right:8px;position:absolute;top:0px;color:yellow;'>
		              <?php if ($giatri['timecurrent']<($giatri['timedatabase'])) {?>
		              	<img src='images/circle_green.png' style='width:13px;height:13px;padding-top:4px;'>
		              	<?php }else if ($giatri['timecurrent']>($giatri['timedatabase'])){?>
		              		<img src='images/circle_red.png' style='width:13px;height:13px;padding-top:4px;'>
		              <?php }?>
		              </span>
		              <span style='left:8px;position:absolute;top:150px;color:yellow;font-size:23px;'><?=$giatri['datetime']?></span>
		              <span style='left:8px;position:absolute;top:184px;color:yellow;font-size:23px'><?=$giatri['platecar']?></span>
		              <span style='right:8px;position:absolute;top:114px;color:yellow;font-size:23px;'><?=$giatri['distance']?></span>
		              <span style='right:8px;position:absolute;top:150px;color:yellow;font-size:23px'><?=$giatri['trigertype']?></span>
		              <span style='right:8px;position:absolute;top:184px;color:yellow;font-size:23px'><?=($vitri+1)?></span>
	              </td>
         <?php 
           $vitri++;
           if($vitri%$cot==0)
         	echo "</tr>";
		}
			echo "</table>";
			echo "</p>\n";
		}else if (isset($_REQUEST['monitor16'])){
			echo "<table id='tb'>";
		 $height=50;
		
		 $tongso=16;
		 $cot=4;
		 $vitri=0;
		 foreach( $mangcha as $khoa=>$giatri )
		{
			if($vitri%$cot==0)
			echo "<tr  height='".$height."'>";
			?>
				
	              <td style='padding-right:5px;display:block;position:relative;float:left;width: 381px;'>
		              <img src="<?=$giatri['image']?>"  style='width:381px;height:162px'>
		              <span style='left:8px;position:absolute;top:0px;color:yellow;font-size:18px;padding-top:4px;'><?php echo $giatri['carname']?></span>
		              <span style='right:8px;position:absolute;top:0px;color:yellow;'>
		              <?php if ($giatri['timecurrent']<($giatri['timedatabase'])) {?>
		              	<img src='images/circle_green.png' style='width:13px;height:13px;padding-top:4px;'>
		              	<?php }else if ($giatri['timecurrent']>($giatri['timedatabase'])){?>
		              		<img src='images/circle_red.png' style='width:13px;height:13px;padding-top:4px;'>
		              <?php }?>
		              </span>
		              <span style='left:8px;position:absolute;top:109px;color:yellow;font-size:18px;'><?=$giatri['datetime']?></span>
		              <span style='left:8px;position:absolute;top:137px;color:yellow;font-size:18px'><?=$giatri['platecar']?></span>
		              <span style='right:8px;position:absolute;top:79px;color:yellow;font-size:18px;'><?=$giatri['distance']?></span>
		              <span style='right:8px;position:absolute;top:109px;color:yellow;font-size:18px'><?=$giatri['trigertype']?></span>
		              <span style='right:8px;position:absolute;top:137px;color:yellow;font-size:18px'><?=($vitri+1)?></span>
	              </td>
         <?php 
           $vitri++;
           if($vitri%$cot==0)
         	echo "</tr>";
		}
			echo "</table>";
			echo "</p>\n";
		}else if (isset($_REQUEST['monitor25'])){
			echo "<table id='tb'>";
		 $height=50;
		
		 $tongso=25;
		 $cot=5;
		 $vitri=0;
		 foreach( $mangcha as $khoa=>$giatri )
		{
			if($vitri%$cot==0)
			echo "<tr  height='".$height."'>";
			?>
				
	              <td style='width:305px;padding-right:5px;display:block;position:relative;float:left'>
		              <img src="<?=$giatri['image']?>"  style='width:305px;height:130px'>
		              <span style='left:8px;position:absolute;top:0px;color:yellow;font-size:16px;padding-top:4px;'><?php echo $giatri['carname']?></span>
		              <span style='right:8px;position:absolute;top:0px;color:yellow;'>
		              <?php if ($giatri['timecurrent']<($giatri['timedatabase'])) {?>
		              	<img src='images/circle_green.png' style='width:13px;height:13px;padding-top:4px;'>
		              	<?php }else if ($giatri['timecurrent']>($giatri['timedatabase'])){?>
		              		<img src='images/circle_red.png' style='width:13px;height:13px;padding-top:4px;'>
		              <?php }?>
		              </span>
		              <span style='left:8px;position:absolute;top:85px;color:yellow;font-size:16px;'><?=$giatri['datetime']?></span>
		              <span style='left:8px;position:absolute;top:108px;color:yellow;font-size:16px;'><?=$giatri['platecar']?></span>
		              <span style='right:8px;position:absolute;top:60px;color:yellow;font-size:16px;'><?=$giatri['distance']?></span>
		              <span style='right:8px;position:absolute;top:85px;color:yellow;font-size:16px;'><?=$giatri['trigertype']?></span>
		              <span style='right:8px;position:absolute;top:108px;color:yellow;font-size:16px;'><?=($vitri+1)?></span>
	              </td>
         <?php 
           $vitri++;
           if($vitri%$cot==0)
         	echo "</tr>";
		}
			echo "</table>";
			echo "</p>\n";
		}else if (isset($_REQUEST['monitor36'])){
			echo "<table id='tb'>";
		 $height=50;
		
		 $tongso=36;
		 $cot=6;
		 $vitri=0;
		 foreach( $mangcha as $khoa=>$giatri )
		{
			if($vitri%$cot==0)
			echo "<tr  height='".$height."'>";
			?>
				
	              <td style='width:254px;padding-right:5px;display:block;position:relative;float:left'>
		              <img src="<?=$giatri['image']?>" style='width:254px;height:105px'>
		              <span style='left:8px;position:absolute;top:0px;color:yellow;font-size:15px;;padding-top:4px;'><?php echo $giatri['carname']?></span>
		              <span style='right:8px;position:absolute;top:0px;color:yellow;'>
		              <?php if ($giatri['timecurrent']<($giatri['timedatabase'])) {?>
		              	<img src='images/circle_green.png' style='width:13px;height:13px;padding-top:4px;'>
		              	<?php }else if ($giatri['timecurrent']>($giatri['timedatabase'])){?>
		              		<img src='images/circle_red.png' style='width:13px;height:13px;padding-top:4px;'>
		              <?php }?>
		              </span>
		              <span style='left:8px;position:absolute;top:65px;color:yellow;font-size:15px;'><?=$giatri['datetime']?></span>
		              <span style='left:8px;position:absolute;top:84px;color:yellow;font-size:15px;'><?=$giatri['platecar']?></span>
		              <span style='right:8px;position:absolute;top:50px;color:yellow;font-size:15px;'><?=$giatri['distance']?></span>
		              <span style='right:8px;position:absolute;top:65px;color:yellow;font-size:15px;'><?=$giatri['trigertype']?></span>
		              <span style='right:8px;position:absolute;top:84px;color:yellow;font-size:15px;'><?=($vitri+1)?></span>
	              </td>
         <?php 
           $vitri++;
           if($vitri%$cot==0)
         	echo "</tr>";
		}
			echo "</table>";
			echo "</p>\n";
		}else if (isset($_REQUEST['monitor49'])){
			echo "<table id='tb'>";
		 $height=50;
		
		 $tongso=49;
		 $cot=7;
		 $vitri=0;
		 foreach( $mangcha as $khoa=>$giatri )
		{
			if($vitri%$cot==0)
			echo "<tr  height='".$height."'>";
			?>
				
	              <td style='width:215px;padding-right:5px;display:block;position:relative;float:left'>
		              <img src="<?=$giatri['image']?>" style='width:215px;height:96px'>
		              <span style='left:8px;position:absolute;top:0px;color:yellow;font-size:14px;padding-top:4px;'><?php echo $giatri['carname']?></span>
		              <span style='right:8px;position:absolute;top:0px;color:yellow;'>
		              <?php if ($giatri['timecurrent']<($giatri['timedatabase'])) {?>
		              	<img src='images/circle_green.png' style='width:13px;height:13px;padding-top:4px;'>
		              	<?php }else if ($giatri['timecurrent']>($giatri['timedatabase'])){?>
		              		<img src='images/circle_red.png' style='width:13px;height:13px;padding-top:4px;'>
		              <?php }?>
		              </span>
		              <span style='left:8px;position:absolute;top:55px;color:yellow;font-size:14px;'><?=$giatri['datetime']?></span>
		              <span style='left:8px;position:absolute;top:74px;color:yellow;font-size:14px;'><?=$giatri['platecar']?></span>
		              <span style='right:8px;position:absolute;top:40px;color:yellow;font-size:14px;'><?=$giatri['distance']?></span>
		              <span style='right:8px;position:absolute;top:55px;color:yellow;font-size:14px;'><?=$giatri['trigertype']?></span>
		              <span style='right:8px;position:absolute;top:74px;color:yellow;font-size:14px;'><?=($vitri+1)?></span>
	              </td>
         <?php 
           $vitri++;
           if($vitri%$cot==0)
         	echo "</tr>";
		}
			echo "</table>";
			echo "</p>\n";
		}
		else if (isset($_REQUEST['monitor64'])){
		
			 echo "<table id='tb'>";
			 $height=50;
			
			 $tongso=64;
			 $cot=8;
			 $vitri=0;
			 foreach( $mangcha as $khoa=>$giatri )
			{
				if($vitri%$cot==0)
				echo "<tr  height='".$height."'>";
				?>
					
		              <td style='padding-right:5px;width:188px;display:block;position:relative;float:left'>
			              <img src="<?=$giatri['image']?>" style='width:188px;height:80px'>
			              <span style='left:8px;position:absolute;top:0px;color:yellow;font-size:11px;padding-top:4px;'><?php echo $giatri['carname']?></span>
			              <span style='right:8px;position:absolute;top:0px;color:yellow;'>
			              <?php if ($giatri['timecurrent']<($giatri['timedatabase'])) {?>
			              	<img src='images/circle_green.png' style='width:13px;height:13px;padding-top:4px;'>
			              	<?php }else if ($giatri['timecurrent']>($giatri['timedatabase'])){?>
			              		<img src='images/circle_red.png' style='width:13px;height:13px;padding-top:4px;'>
			              <?php }?>
			              </span>
			              <span style='left:8px;position:absolute;top:55px;color:yellow;font-size:11px;'><?=$giatri['datetime']?></span>
			              <span style='left:8px;position:absolute;top:68px;color:yellow;font-size:11px;'><?=$giatri['platecar']?></span>
			              <span style='right:8px;position:absolute;top:40px;color:yellow;font-size:11px;'><?=$giatri['distance']?></span>
			              <span style='right:8px;position:absolute;top:55px;color:yellow;font-size:11px;'><?=$giatri['trigertype']?></span>
			              <span style='right:8px;position:absolute;top:68px;color:yellow;font-size:11px;'><?=($vitri+1)?></span>
		              </td>
	         <?php 
	           $vitri++;
	           if($vitri%$cot==0)
	         	echo "</tr>";
		}
		
			echo "</table>";
			echo "</p>\n";
		}
?>